import { authorizations } from '@/api/login'
import { Message } from 'element-ui'
import router from '@/router'
import { myUserinfo } from '@/api/user'
export default {
  namespaced: true,
  state () {
    return {
      token: '',
      userInfo: ''
    }
  },
  getters: {},
  mutations: {
    setToken (state, val) {
      state.token = val
    },
    // 保存userInfo
    setUserInfo (state, val) {
      state.userInfo = val
    }
  },
  actions: {
    // 登录
    async login (ctx, info) {
      const res = await authorizations(info)
      console.log(res)
      ctx.commit('setToken', res.token)
      Message.success('登录成功')
      router.push('/layout')
    },
    async regUserInfo (ctx) {
      if (!ctx.state.userInfo.id) {
        const res = await myUserinfo()
        console.log(res)
        ctx.commit('setUserInfo', res.data)
      }
    },
    logout (ctx) {
      ctx.commit('setToken', {})
      ctx.commit('setUserInfo', {})
      router.push('/login')
    }
  },
  modules: {}
}
